package com.lanou.zy.projecta.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.activity.RoundWorldActivity;
import com.lanou.zy.projecta.util.OnItemClickListener;
import com.lanou.zy.projecta.volley.VolleySingleton;
import com.lanou.zy.projecta.entity.Community;
import com.lanou.zy.projecta.util.MyGridView;

import java.util.List;

/**
 * Created by dllo on 15/11/2.
 */
public class CommunityExpandableListViewAdapter extends BaseExpandableListAdapter {
    private List<Community.DataEntity.ForumListEntity> parent;
    private ImageLoader imageLoader;
    private Context context;
    private LayoutInflater inflater;
    private List<Community.DataEntity.ForumListEntity.GroupEntity> entities;

    public CommunityExpandableListViewAdapter(Context c) {
        this.context = c;
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    public void addData(List<Community.DataEntity.ForumListEntity> cdf) {
        this.parent = cdf;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return parent != null && parent.size() > 0 ? parent.size() : 0;
    }

    // 获取当前父item下子item的个数
    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return parent.get(groupPosition);
    }

    // 得到子item需要关联的数据
    @Override
    public List<Community.DataEntity.ForumListEntity.GroupEntity> getChild(int groupPosition, int childPosition) {
        return parent.get(groupPosition).getGroup();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    // 得到子item需要关联的数据
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    public class CommunityParentViewHolder {
        TextView communityHeaderTv;
    }


    // 设置父item的组件
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup viewGroup) {

        CommunityParentViewHolder holder = null;
        if (convertView == null) {
            holder = new CommunityParentViewHolder();
            inflater = LayoutInflater.from(viewGroup.getContext());
            convertView = inflater.inflate(R.layout.community_parents_expandablelistview_item, null);
            holder.communityHeaderTv = (TextView) convertView.findViewById(R.id.communityHeaderTv);
            convertView.setTag(holder);

        } else {
            holder = (CommunityParentViewHolder) convertView.getTag();
        }

        Community.DataEntity.ForumListEntity cdf = (Community.DataEntity.ForumListEntity) getGroup(groupPosition);
        if (cdf != null) {
            holder.communityHeaderTv.setText(cdf.getName());
        }
        return convertView;
    }


    // 设置子item的组件
    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View v, ViewGroup viewGroup) {
        MyGridView communityGridView;
//        if (v == null) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        v = inflater.inflate(R.layout.community_child_expandablelistview_item, null);

        communityGridView = (MyGridView) v.findViewById(R.id.communityGridView);

        CommunityGridViewAdapter communityGridViewAdapter = new CommunityGridViewAdapter(context, getChild(groupPosition, childPosition));
        communityGridView.setAdapter(communityGridViewAdapter);

        communityGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> p, View view, int position, long id) {
                Intent intent = new Intent(context, RoundWorldActivity.class);
                String str = getChild(groupPosition, childPosition).get(position).getId() + "";
                String content = getChild(groupPosition, childPosition).get(position).getTotal_threads() + "";
                String photo = getChild(groupPosition, childPosition).get(position).getPhoto() + "";
                String title = getChild(groupPosition, childPosition).get(position).getName() + "";
                intent.putExtra("id", str);
                intent.putExtra("content", content);
                intent.putExtra("photo", photo);
                intent.putExtra("title", title);
                context.startActivity(intent);
            }
        });

        return v;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


}
